package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //--1-- 添加购物车  请求url地址：/shoppingCart/add
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        log.info("[>>>--<购物车模块>添加请求JSON]: shoppingCart={}",shoppingCart);
        //--1--ID  Long userId = BaseContext.getCurrentId();
        User user =(User)session.getAttribute("user");
        Long userId = user.getId();


        //--2--  条件查询--是否点了，该菜品或套餐
        LambdaQueryWrapper<ShoppingCart> qwr=new LambdaQueryWrapper<>();
        qwr.eq(ShoppingCart::getUserId,userId);
        //"dishId"  "setmealId" 两个存一个
        if(shoppingCart.getDishId()!=null){
            //  +菜品
            qwr.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //  +套餐
            qwr.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qwr);

        if(one==null){
            //--第一次点，新增到表  数量number=1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            log.info("[>>>--<购物车模块>新增一条数据]: shoppingCart={}",shoppingCart);
            shoppingCartService.save(shoppingCart);
        }else {
            //--已有对象，更新
            one.setNumber(one.getNumber()+1);
            one.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(one);
            shoppingCart=one;
        }
        return R.success(shoppingCart);
    }



    //--2-- 查询购物车  请求url地址：/shoppingCart/list
    @GetMapping("/list")
    public R<List<ShoppingCart>> mselectlist(){
        log.info("[>>>--<购物车模块>查询购物]:");
        return R.success(
                shoppingCartService.list(
                        new LambdaQueryWrapper<ShoppingCart>()
                                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                                .orderByDesc(ShoppingCart::getCreateTime)));
        //return R.success(null);     //不可为null，页面不展示
    }


    //--3-- 取消购物车  请求url地址：/shoppingCart/sub
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        log.info("[>>>--<购物车模块>取消请求JSON]: shoppingCart={}",shoppingCart);
        //--1-- 当前用户id
        User user = (User) session.getAttribute("user");
        Long userId = user.getId();

        //--2--  "dishId": 还是 "setmealId":
        LambdaQueryWrapper<ShoppingCart> qwr=new LambdaQueryWrapper<>();
        qwr.eq(ShoppingCart::getUserId,userId);
        if(shoppingCart.getDishId()!=null){
            //  -菜品
            qwr.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //  -套餐
            qwr.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(qwr);

        if(cartServiceOne!=null){
            //购物车--当前有数据
            if(cartServiceOne.getNumber()>1){
                //当前数据的份数大于1份，每次减1
                cartServiceOne.setNumber(cartServiceOne.getNumber()-1);
                shoppingCartService.updateById(cartServiceOne);
            }else {
                //当前数据的份数为1份，清除数据
                cartServiceOne.setNumber(0);
                shoppingCartService.removeById(cartServiceOne);
            }
        }
        return R.success(cartServiceOne);

    }




    //--4-- 清空购物车  请求url地址：/shoppingCart/clean
    @DeleteMapping("/clean")
    public R<String> mydeleteList(){
        shoppingCartService.remove(
                new LambdaQueryWrapper<ShoppingCart>()
                        .eq(ShoppingCart::getUserId,BaseContext.getCurrentId()));
        return R.success("清空购物车成功");
    }



}
